<<<<<<< HEAD
import pickle
import sys
import hashlib
import os
from lib import commen


class Course:
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period
        self.teacher = None


class Student:
    c_l = [("show_courses", "查看所有课程"),
           ("select_courses", "选择课程"),
           ("show_selected_course", "查看所选课程"),
           ("exit", "退出程序")]

    def __init__(self, name):
        self.name = name
        self.courses = []

    def show_courses(self):
        with open("../db/Course", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'课程名:{getattr(obj, "name")}, 价格:{getattr(obj, "price")}元,周期:{getattr(obj, "period")}天')
                except Exception:
                    break

    def select_courses(self):
        self.show_courses()
        course = input("请输入要选择课程的名称").strip()
        self.courses.append(course)
        with open("../db/Student", mode="rb") as f1, open("../db/new", mode="wb") as f2:
            while 1:
                try:
                    i = pickle.load(f1)
                    if self.name == i.name:
                        i = self
                    pickle.dump(i, f2)
                except Exception:
                    break
        os.remove("../db/Student")
        os.rename("../db/new", "../db/Student")

    def show_selected_course(self):
        for n, c in enumerate(self.courses, 1):
            print(n, c)

    def exit(self):
        sys.exit()


class Manager:
    c_l = [("create_course", "创建课程"),
           ("create_student", "创建学生账号"),
           ("show_courses", "查看所有课程"),
           ("shou_stuents", "查看所有学生"),
           ("show_students_courses", "查看所有学生的选课情况"),
           ("exit", "退出程序")]

    def __init__(self, name):
        self.name = name

    def create_course(self):
        name = input("请输入课程名字")
        price = input("请输入课程价格")
        period = input("请输入课程周期")
        name = Course(name, price, period)
        with open("../db/Course", mode="ab") as f1:
            pickle.dump(name, f1)
        commen.logger(f"创建了课程{name.name}")

    def create_student(self):
        name = input("请输入学生名字")
        name = Student(name)
        with open("../db/Student", mode="ab") as f1:
            pickle.dump(name, f1)
        with open("../db/Userinfo", mode="a", encoding="utf-8") as f1:
            ret = hashlib.md5()
            ret.update('1234567&'.encode('utf-8'))
            f1.write(f"\n{name.name}|{ret.hexdigest()}|Student")
        commen.logger(f"创建了学生{name.name}")

    def show_courses(self):
        with open("../db/Course", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'课程名:{getattr(obj, "name")}, 价格:{getattr(obj, "price")}元,周期:{getattr(obj, "period")}天')
                except Exception:
                    break

    def shou_stuents(self):
        with open("../db/Student", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'{obj.name}')
                except Exception:
                    break

    def show_students_courses(self):
        with open("../db/Student", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'{getattr(obj, "name")}选择的课程为:{getattr(obj, "courses")}')
                except Exception:
                    break

    def exit(self):
        sys.exit()


def login():
    count = 3
    while count > 0:
        user_name = input("请输入你账号:")
        pass_word = input("请输入你的密码:")
        ret = hashlib.md5()
        ret.update(pass_word.encode('utf-8'))
        pass_word_md5 = ret.hexdigest()
        with open("../db/Userinfo", mode="r", encoding="utf-8") as f1:
            for i in f1:
                li = i.strip().split("|")
                if user_name == li[0] and pass_word_md5 == li[1]:
                    print("登录成功")
                    return li
            count -= 1
            print(f"账号或密码错误,剩余机会{count}")
    return False


def run():
    flag = login()

    while flag:
        if flag[2] == "Manager":
            obj = Manager("kevin")
            for n, item in enumerate(obj.c_l, 1):
                print(n, item[1])
            choice = int(input("请输入你的选择的序号:"))
            if hasattr(obj, obj.c_l[choice - 1][0]):
                getattr(obj, obj.c_l[choice - 1][0])()
        elif flag[2] == "Student":
            with open("../db/Student", mode="rb") as f1:
                while 1:
                    try:
                        i = pickle.load(f1)
                        if flag[0] == i.name:
                            obj = i
                    except Exception:
                        break
            for n, item in enumerate(obj.c_l, 1):
                print(n, item[1])
            choice = int(input("请输入你的选择的序号:"))
            if hasattr(obj, obj.c_l[choice - 1][0]):
                getattr(obj, obj.c_l[choice - 1][0])()
    else:
        print("登录失败")




=======
import pickle
import sys
import hashlib
import os
from lib import commen


class Course:
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period
        self.teacher = None


class Student:
    c_l = [("show_courses", "查看所有课程"),
           ("select_courses", "选择课程"),
           ("show_selected_course", "查看所选课程"),
           ("exit", "退出程序")]

    def __init__(self, name):
        self.name = name
        self.courses = []

    def show_courses(self):
        with open("../db/Course", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'课程名:{getattr(obj, "name")}, 价格:{getattr(obj, "price")}元,周期:{getattr(obj, "period")}天')
                except Exception:
                    break

    def select_courses(self):
        self.show_courses()
        course = input("请输入要选择课程的名称").strip()
        self.courses.append(course)
        with open("../db/Student", mode="rb") as f1, open("../db/new", mode="wb") as f2:
            while 1:
                try:
                    i = pickle.load(f1)
                    if self.name == i.name:
                        i = self
                    pickle.dump(i, f2)
                except Exception:
                    break
        os.remove("../db/Student")
        os.rename("../db/new", "../db/Student")

    def show_selected_course(self):
        for n, c in enumerate(self.courses, 1):
            print(n, c)

    def exit(self):
        sys.exit()


class Manager:
    c_l = [("create_course", "创建课程"),
           ("create_student", "创建学生账号"),
           ("show_courses", "查看所有课程"),
           ("shou_stuents", "查看所有学生"),
           ("show_students_courses", "查看所有学生的选课情况"),
           ("exit", "退出程序")]

    def __init__(self, name):
        self.name = name

    def create_course(self):
        name = input("请输入课程名字")
        price = input("请输入课程价格")
        period = input("请输入课程周期")
        name = Course(name, price, period)
        with open("../db/Course", mode="ab") as f1:
            pickle.dump(name, f1)
        commen.logger(f"创建了课程{name.name}")

    def create_student(self):
        name = input("请输入学生名字")
        name = Student(name)
        with open("../db/Student", mode="ab") as f1:
            pickle.dump(name, f1)
        with open("../db/Userinfo", mode="a", encoding="utf-8") as f1:
            ret = hashlib.md5()
            ret.update('1234567&'.encode('utf-8'))
            f1.write(f"\n{name.name}|{ret.hexdigest()}|Student")
        commen.logger(f"创建了学生{name.name}")

    def show_courses(self):
        with open("../db/Course", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'课程名:{getattr(obj, "name")}, 价格:{getattr(obj, "price")}元,周期:{getattr(obj, "period")}天')
                except Exception:
                    break

    def shou_stuents(self):
        with open("../db/Student", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'{obj.name}')
                except Exception:
                    break

    def show_students_courses(self):
        with open("../db/Student", mode="rb") as f1:
            while 1:
                try:
                    obj = pickle.load(f1)
                    print(f'{getattr(obj, "name")}选择的课程为:{getattr(obj, "courses")}')
                except Exception:
                    break

    def exit(self):
        sys.exit()


def login():
    count = 3
    while count > 0:
        user_name = input("请输入你账号:")
        pass_word = input("请输入你的密码:")
        ret = hashlib.md5()
        ret.update(pass_word.encode('utf-8'))
        pass_word_md5 = ret.hexdigest()
        with open("../db/Userinfo", mode="r", encoding="utf-8") as f1:
            for i in f1:
                li = i.strip().split("|")
                if user_name == li[0] and pass_word_md5 == li[1]:
                    print("登录成功")
                    return li
            count -= 1
            print(f"账号或密码错误,剩余机会{count}")
    return False


def run():
    flag = login()

    while flag:
        if flag[2] == "Manager":
            obj = Manager("kevin")
            for n, item in enumerate(obj.c_l, 1):
                print(n, item[1])
            choice = int(input("请输入你的选择的序号:"))
            if hasattr(obj, obj.c_l[choice - 1][0]):
                getattr(obj, obj.c_l[choice - 1][0])()
        elif flag[2] == "Student":
            with open("../db/Student", mode="rb") as f1:
                while 1:
                    try:
                        i = pickle.load(f1)
                        if flag[0] == i.name:
                            obj = i
                    except Exception:
                        break
            for n, item in enumerate(obj.c_l, 1):
                print(n, item[1])
            choice = int(input("请输入你的选择的序号:"))
            if hasattr(obj, obj.c_l[choice - 1][0]):
                getattr(obj, obj.c_l[choice - 1][0])()
    else:
        print("登录失败")




>>>>>>> 00
